package controller

import (
	"net/http"
	"smart-flow/server/config"
	"smart-flow/server/proto"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

var (
	Token string
)

func Home(c *gin.Context) {
	c.HTML(http.StatusOK, "home.html", nil)
}

func Login(c *gin.Context) {
	var req proto.LoginReq
	if c.ShouldBind(&req) != nil {
		c.JSON(http.StatusBadRequest, proto.NewCommonResp(proto.ParameterError))
		return
	}
	if strings.TrimSpace(req.Passwd) != config.AppConfig().Server.Passwd {
		c.JSON(http.StatusBadRequest, proto.NewCommonResp(proto.ParameterError))
		return
	}
	Token = uuid.New().String()
	c.SetCookie("token", Token, 3600*24, "/", c.Request.Host, false, true)
	c.JSON(http.StatusOK, proto.NewCommonResp(proto.Success))
}
